import { createRouter, createWebHashHistory } from "vue-router";
import layout from "../views/layout/layout.vue";
import { useCountStore } from "@/store/count";



const router = createRouter({
    // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    history: createWebHashHistory(),
    routes: [
        {
            path: '/zhuce',
            name: 'zhuce',
            component: () => import('../views/login/zuc.vue')
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/login/login.vue')
        },
        {
            path: '/:catchAll(.*)',
            name: '404',
            component: () => import('../views/404/404.vue')
        },
        {
            path: '/menu',
            meta: {
                title: "首页",
                up: true
            },
            component: layout,
            children: [
                {
                    path: 'index',
                    meta: {
                        title: "首页"
                    },
                    component: () => import('@/views/index/index.vue'),
                }
            ]
        },
        {
            path: '/info',
            component: layout,
            meta: {
                title: "登录日志",
                qx: "admin"
            },
            children: [
                {
                    path: 'info',
                    meta: {
                        title: "登录日志信息"
                    },
                    component: () => import('@/views/info/info.vue'),
                }
            ]
        },
        {
            path: '/me',
            component: layout,
            meta: {
                title: "个人信息",
                qx: "user"
            },
            children: [
                {
                    path: 'useme',
                    meta: {
                        title: "个人信息操作"
                    },
                    component: () => import('@/views/me/useme.vue'),
                }
            ]
        },
        {
            path: '/userdata',
            component: layout,
            meta: {
                title: "用户信息",
                qx: "admin"
            },
            children: [
                {
                    path: 'uuserdata',
                    meta: {
                        title: "用户信息操作"
                    },
                    component: () => import('@/views/me/userdata.vue'),
                }
            ]
        }
        ,
        {
            path: '/items',
            component: layout,
            meta: {
                title: "投票信息管理",
                qx: "admin"
            },
            children: [
                {
                    path: 'useritem',
                    meta: {
                        title: "投票信息操作"
                    },
                    component: () => import('@/views/tou/allitem.vue'),
                }
            ]
        },
        {
            path: '/additems',
            component: layout,
            meta: {
                title: "创建投票",
                qx: "uadmin"
            },
            children: [
                {
                    path: 'additems',
                    meta: {
                        title: "创建投票"
                    },
                    component: () => import('@/views/tou/chuang.vue'),
                },
                {
                    path: 'item',
                    meta: {
                        title: "已创建(个人)"
                    },
                    component: () => import('@/views/tou/usecitem.vue'),
                }
            ]
        }
        ,
        {
            path: '/allitems',
            component: layout,
            meta: {
                title: "所有投票",
                qx: "user"
            },
            children: [
                {
                    path: 'item',
                    meta: {
                        title: "投票操作(参与/查看)"
                    },
                    component: () => import('@/views/tou/useitem.vue'),
                },
                {
                    path: 'items',
                    meta: {
                        title: "已参加"
                    },
                    component: () => import('@/views/tou/canitem.vue'),
                }
            ]
        }
        ,
        {
            path: '/fk',
            component: layout,
            meta: {
                title: "在线反馈",
                qx: "user"
            },
            children: [
                {
                    path: 'fk',
                    meta: {
                        title: "反馈评论(参与/查看)"
                    },
                    component: () => import('@/views/tip/fk.vue'),
                }
            ]
        },
        {
            path: '/gong',
            component: layout,
            meta: {
                title: "公告",
                qx: "admin"
            },
            children: [
                {
                    path: 'gao',
                    meta: {
                        title: "公告管理"
                    },
                    component: () => import('@/views/tip/gao.vue'),
                }
            ]
        }

    ] // `routes: routes` 的缩写
})
router.beforeEach(async (to, from) => {
    console.log("Tttttt", to.path);

    const qxstore = useCountStore()
    console.log("qqqqqqq", qxstore.qx);

    if (!window.localStorage.getItem('token') && to.path != '/login') {

        if (to.path !== '/zhuce') {
            console.log("zl");

            // 将用户重定向到登录页面
            return { path: '/login' }
        }
    } else if (to.path == '/') {
        return { path: '/menu/index' }
    } else if (to.meta.qx == "admin") {
        setTimeout(() => {
            if (qxstore.qx != "admin") {
                return { path: '/menu/index' }
            }
        }, 500);
    }
})
export default router